package com.jf.demo;

import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackAndroid;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Registration;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.sys.android.util.DialogFactory;
import com.sys.android.util.FinalUntil;
import com.sys.android.xmppmanager.XmppConnection;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

@ContentView(R.layout.registernext)
public class RegisterUserNext extends Activity {

	@ViewInject(R.id.editText1)
	public EditText edText1;

	@ViewInject(R.id.personPhone)
	public EditText edText2;

	private SharedPreferences useShare;

	private Editor editor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		ViewUtils.inject(this);// xml视图加载的处理

	}
	
	public void passwordConfirmation(View view) {
		final Dialog dialog = DialogFactory.creatRequestDialog(
				RegisterUserNext.this, "请稍后……");
		dialog.show();
		useShare = getSharedPreferences("userInfo", MODE_PRIVATE);
		editor = useShare.edit();

		Registration reg = new Registration();
		reg.setType(IQ.Type.SET);
		reg.setTo(XmppConnection.getConnection().getServiceName());
		reg.setUsername(useShare.getString("username", "0"));
		
		reg.setPassword(edText2.getText().toString());
		reg.addAttribute("name", "快递通客户端");
		reg.addAttribute("email", "");

		reg.addAttribute("android", "geolo_createUser_android");
		PacketFilter filter = new AndFilter(new PacketIDFilter(
				reg.getPacketID()), new PacketTypeFilter(IQ.class));
		PacketCollector collector = XmppConnection.getConnection()
				.createPacketCollector(filter);
		XmppConnection.getConnection().sendPacket(reg);
		IQ result = (IQ) collector.nextResult(SmackConfiguration
				.getPacketReplyTimeout());
		// Stop queuing results
		collector.cancel();// 停止请求results（是否成功的结果）

		if (result == null) {
			dialog.hide();
			Toast.makeText(getApplicationContext(), "服务器没有返回结果",
					Toast.LENGTH_SHORT).show();
		} else if (result.getType() == IQ.Type.ERROR) {
			dialog.hide();
			if (result.getError().toString().equalsIgnoreCase("conflict(409)")) {
				Toast.makeText(getApplicationContext(), "这个账号已经存在",
						Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(getApplicationContext(), "注册失败",
						Toast.LENGTH_SHORT).show();
			}
		} else if (result.getType() == IQ.Type.RESULT) {
			// SharedPreferences是Android平台上一个轻量级的存储类，主要是保存一些常用的配置,
			// 这些信息以XML文件的形式保存在 /data/data/PACKAGE_NAME /shared_prefs 目录下
			try {

				XmppConnection.getConnection().login(
						useShare.getString("username", "0"),
						edText2.getText().toString());
				// 连接服务器成功，更改在线状态
				Presence presence = new Presence(Presence.Type.available);
				XmppConnection.getConnection().sendPacket(presence);

			} catch (Exception e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
			}
			editor.putString("password", edText2.getText().toString());
			editor.commit();
			httpUserRe(useShare.getString("username", "0"), edText2.getText()
					.toString());
			Toast.makeText(view.getContext(), "注册成功", Toast.LENGTH_LONG).show();
			Intent intent = new Intent(RegisterUserNext.this, Mainmenu.class);
			startActivity(intent);
			this.finish();
		}

	}
	/**单独的请求，提交到服务，注册*/
	public void httpUserRe(String username, String password) {
		RequestParams params = new RequestParams();

		params.addQueryStringParameter("userID", username);
		params.addQueryStringParameter("password", password);
		params.addQueryStringParameter("openfireID", username);
		
		HttpUtils http = new HttpUtils();
		http.send(HttpMethod.POST,
				FinalUntil.ServerAddress+"mobileRegistered", params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						Log.i("RegisterUserNext", arg1);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// arg0.result返回服务器结果
						Log.i("RegisterUserNext", arg0.result);
					}
				});
		
	}

}
